<template>
  <div class="report-form-content">
    <van-tabs :active="active">
      <van-tab title="基本信息">
        <van-collapse v-model="activeNames">
          <van-collapse-item title="基本信息" name="1">
            <div>
              <div class="shenpilist">
                <label>合同编号：</label>
                <span>{{ obj.code }}</span>
              </div>
              <div class="shenpilist">
                <label>合同名称：</label>
                <span>{{ obj.name }}</span>
              </div>
              <div class="shenpilist">
                <label>签订人：</label>
                <span>{{ showIdLabel(contractSignerList, obj.contractSignerId, 'name') }}</span>
              </div>
              <div class="shenpilist">
                <label>合同类型：</label>
                <span>{{ showIdLabel(contractTypeList, obj.type, 'name') }}</span>
              </div>
              <div class="shenpilist">
                <label>负责人：</label>
                <span>{{ showIdLabel(userList, obj.userId, 'name') }}</span>
              </div>
              <div class="shenpilist">
                <label>合同金额：</label>
                <span>{{ obj.amount }}</span>
              </div>
              <div class="shenpilist">
                <label>合同起始时间：</label>
                <span>{{ obj.contractLaunch }}</span>
              </div>
              <div class="shenpilist">
                <label>合同终止时间：</label>
                <span>{{ obj.contractComplete }}</span>
              </div>
              <div class="shenpilist">
                <label>是否需要验收：</label>
                <span>
                  <EnumTag :enum-list="getEnumList('CommonYesNo')" :enum-key="obj.needInspection" />
                </span>
              </div>
            </div>
          </van-collapse-item>
          <van-collapse-item title="甲方信息" name="2">
            <div class="update-log" v-for="(item,index) in partyAList" :key="index">
              <div class="report-form-list-content">
                <div class="shenpilist">
                  <label>参与方类型：</label>
                  <span>
                    <DictTag :dict-list="getDictList('ContractParticipationUserType')" :dict-key="item.participationUserType" />
                  </span>
                </div>
                <div class="shenpilist">
                  <label>单位名称：</label>
                  <span>{{ item.name }}</span>
                </div>
                <div class="shenpilist">
                  <label>纳税识别号：</label>
                  <span>{{ item.identification }}</span>
                </div>
                <div class="shenpilist">
                  <label>联系人：</label>
                  <span>{{ item.contact }}</span>
                </div>
                <div class="shenpilist">
                  <label>联系手机号：</label>
                  <span>{{ item.contactMobile }}</span>
                </div>
                <div class="shenpilist">
                  <label>收款账号：</label>
                  <span>{{ item.proceedsAccount }}</span>
                </div>
                <div class="shenpilist">
                  <label>收款银行：</label>
                  <span>{{ showIdLabel(userList, item.bankList, 'bankName') }}</span>
                </div>
              </div>
            </div>
          </van-collapse-item>
          <van-collapse-item title="乙方信息" name="3">
            <div class="update-log" v-for="(item,index) in partyBList" :key="index">
              <div class="report-form-list-content">
                <div class="shenpilist">
                  <label>参与方类型：</label>
                  <span>
                    <DictTag :dict-list="getDictList('ContractParticipationUserType')" :dict-key="item.participationUserType" />
                  </span>
                </div>
                <div class="shenpilist">
                  <label>单位名称：</label>
                  <span>{{ item.name }}</span>
                </div>
                <div class="shenpilist">
                  <label>纳税识别号：</label>
                  <span>{{ item.identification }}</span>
                </div>
                <div class="shenpilist">
                  <label>联系人：</label>
                  <span>{{ item.contact }}</span>
                </div>
                <div class="shenpilist">
                  <label>联系手机号：</label>
                  <span>{{ item.contactMobile }}</span>
                </div>
                <div class="shenpilist">
                  <label>收款账号：</label>
                  <span>{{ item.proceedsAccount }}</span>
                </div>
                <div class="shenpilist">
                  <label>收款银行：</label>
                  <span>{{ showIdLabel(userList, item.bankList, 'bankName') }}</span>
                </div>
              </div>
            </div>
            
          </van-collapse-item>
          <van-collapse-item title="第三方信息" name="4">
            <div class="update-log" v-for="(item,index) in partyCList" :key="index">
              <div class="report-form-list-content">
                <div class="shenpilist">
                  <label>参与方类型：</label>
                  <span>
                    <DictTag :dict-list="getDictList('ContractParticipationUserType')" :dict-key="item.participationUserType" />
                  </span>
                </div>
                <div class="shenpilist">
                  <label>单位名称：</label>
                  <span>{{ item.name }}</span>
                </div>
                <div class="shenpilist">
                  <label>纳税识别号：</label>
                  <span>{{ item.identification }}</span>
                </div>
                <div class="shenpilist">
                  <label>联系人：</label>
                  <span>{{ item.contact }}</span>
                </div>
                <div class="shenpilist">
                  <label>联系手机号：</label>
                  <span>{{ item.contactMobile }}</span>
                </div>
                <div class="shenpilist">
                  <label>收款账号：</label>
                  <span>{{ item.proceedsAccount }}</span>
                </div>
                <div class="shenpilist">
                  <label>收款银行：</label>
                  <span>{{ showIdLabel(userList, item.bankList, 'bankName') }}</span>
                </div>
              </div>
            </div>
          </van-collapse-item>
          <van-collapse-item title="合同信息" name="5">
            <div class="shenpilist">
              <label>合同模版：</label>
              <span>{{ showIdLabel(contractTemplateList, obj.contractTemplateId, 'name') }}</span>
            </div>
            <div class="shenpilist">
              <label>合同内容：</label>
            </div>
            <div class="shenpilist">
              <p v-html="obj.content"></p>
            </div>
          </van-collapse-item>
        </van-collapse>
      </van-tab>
      <van-tab title="验收条件" v-if="obj.needInspection === '1'">
        <div class="" v-if="inspectionsList.length > 0">
          <div class="update-log" v-for="(item,index) in inspectionsList" :key="index">
            <div class="shenpilist">
              <label>验收负责人：</label>
              <span>{{ showIdLabel(userList, item.principalUserId, 'name') }}</span>
            </div>
            <div class="shenpilist">
              <label>验收内容：</label>
              <span>{{ item.content }}</span>
            </div>
            <div class="shenpilist">
              <label>备注：</label>
              <span>{{ item.remark }}</span>
            </div>
          </div>
        </div>
        <van-empty description="暂无收款计划" v-else />
      </van-tab>
      <van-tab title="收付信息">
        <van-collapse v-model="activeNames1">
          <van-collapse-item title="收款计划" name="1">
            <div class="" v-if="payeesList.length > 0">
              <div class="update-log" v-for="(item,index) in payeesList" :key="index">
                <div class="report-form-list-content">
                  <div class="shenpilist">
                    <label>收款批次：</label>
                    <span>{{ item.batchNo }}</span>
                  </div>
                  <div class="shenpilist">
                    <label>收款条件：</label>
                    <span>{{ item.condition }}</span>
                  </div>
                  <div class="shenpilist">
                    <label>约定收款金额：</label>
                    <span>￥{{ item.amount }}</span>
                  </div>
                  <div class="shenpilist">
                    <label>金额占比：</label>
                    <span>{{ item.amountRate }}%</span>
                  </div>
                  <div class="shenpilist">
                    <label>预计收款日期：</label>
                    <span>{{ item.payTime?.substring(0,10) }}</span>
                  </div>
                  <div class="shenpilist">
                    <label>付款单位：</label>
                    <span>{{ item.payCompany }}</span>
                  </div>
                </div>
              </div>
            </div>
            <van-empty description="暂无收款计划" v-else />
          </van-collapse-item>
          <van-collapse-item title="付款计划" name="2">
            <div class="" v-if="paysList.length > 0">
              <div class="update-log" v-for="(item,index) in paysList" :key="index">
                <div class="report-form-list-content">
                  <div class="shenpilist">
                    <label>付款批次：</label>
                    <span>{{ item.batchNo }}</span>
                  </div>
                  <div class="shenpilist">
                    <label>付款条件：</label>
                    <span>{{ item.condition }}</span>
                  </div>
                  <div class="shenpilist">
                    <label>约定支付金额：</label>
                    <span>￥{{ item.amount }}</span>
                  </div>
                  <div class="shenpilist">
                    <label>金额占比：</label>
                    <span>{{ item.amountRate }}%</span>
                  </div>
                  <div class="shenpilist">
                    <label>预计收款日期：</label>
                    <span>{{ item.payeeTime }}</span>
                  </div>
                  <div class="shenpilist">
                    <label>收款单位：</label>
                    <span>{{ item.payeeCompany }}</span>
                  </div>
                </div>
              </div>
            </div>
            <van-empty description="暂无付款计划" v-else />
          </van-collapse-item>
        </van-collapse>
      </van-tab>
      <van-tab title="附件">
        <ul class="files-list" v-if="oFiles.length > 0">
          <li v-for="item in oFiles" :key="item.id">
            <div class="files-list-1">
              <div class="files-list-content">
                <p><span class="list-content-hindline">{{ item.original }}</span> {{ bytesToSize(item.size) }}</p>
                <p><span>上传于 {{ item.createTime }}</span></p>
              </div>
            </div>
          </li>
        </ul>
        <van-empty description="暂无附件" v-else />
      </van-tab>
    </van-tabs>
  </div>
</template>
<script setup>
import {ref, watch} from 'vue'
import {doDownload, doGet} from "@/config/http";
import {svc, showIdLabel, getListPageSize, getEnumList, getDictList, bytesToSize} from "@/config/common";

import attachmentPreview from '@/views/components/base/attachmentPreview.vue'

import { useRoute, useRouter } from 'vue-router'

import initListStore from '@/store/modules/initList'

const props = defineProps({
  contractInitialId: {
    required: true,
    type: String,
  }
})

let route = useRoute()
let router = useRouter()
let active = ref(0)

let activeName = ref('1')
let previewAttachmentId = ref('')
let attachmentVisible = ref(false)
let oper = ref('')
let id = ref('')

let obj = ref({
  amount: '', // 合同金额
  code: '', // 合同编码
  content: '', // 合同内容
  contractComplete: '', // 	合同终止时间
  contractLaunch: '', // 合同起止时间
  contractSignerId: '', // 合同签订人ID
  contractTemplateId: '', // 合同模板ID
  departmentId: '', // 负责部门ID
  id: '', //	主键ID
  name: '', // 合同名称
  needInspection: '', // 是否需要验收
  contractSignerDepartmentId: '', // 合同签订部门ID
  state: '', // 合同状态
  type: '', // 合同类型
  userId: '', // 负责人用户ID
})

let activeNames = ref([])
let activeNames1 = ref([])

let oFiles = ref([])
let inspectionsList = ref([]) // 验收条件
let payeesList = ref([]) // 初始合同收款信息
let paysList = ref([]) // 初始合同付款信息
// let participationsList = ref([]) // 参与方信息

let partyAList = ref([]) // 甲方
let partyBList = ref([]) // 乙方
let partyCList = ref([]) // 第三方

let contractTypeList = initListStore().contractTypeList // 合同类型
let contractCooperationList = initListStore().contractCooperationList // 往来单位
let contractFeetypeList = initListStore().contractFeetypeList // 合同费用类型
let contractTemplateList = initListStore().contractTemplateList // 合同模版
let bankList = initListStore().bankList // 银行
let userList = initListStore().userList // 用户
let governmentList = initListStore().governmentList //  政府单位
let organizationList = initListStore().organizationList // 部门
let contractSignerList = initListStore().contractSignerList // 签订人
let contractFiletypeList = initListStore().contractFiletypeList // 合同附件类型

// const initList = async () => {
//   let p1 = doGet(svc, '/contract-type/page', {pageSize: getListPageSize()}, false) // 合同类型
//   let p2 = doGet(svc, '/contract-cooperation/page', {pageSize: getListPageSize()}, false) // 往来单位
//   let p3 = doGet(svc, '/contract-feetype/page', {pageSize: getListPageSize()}, false) // 合同费用类型
//   let p4 = doGet(svc, '/contract-template/page', {pageSize: getListPageSize()}, false) // 合同模版
//   let p5 = doGet(svc, '/bank/page', {pageSize: getListPageSize()}, false) // 银行
//   let p6 = doGet(svc, '/user/page', {pageSize: getListPageSize()}, false) // 用户
//   let p7 = doGet(svc, '/government/page', {pageSize: getListPageSize()}, false) //  政府单位
//   let p8 = doGet(svc, '/organization/page', {pageSize: getListPageSize()}, false) // 部门
//   let p9 = doGet(svc, '/contract-signer/page', {pageSize: getListPageSize()}, false) // 签订人
//   let p10 = doGet(svc, '/contract-filetype/page', {pageSize: getListPageSize()}, false) // 合同附件类型
//   let resArray = await Promise.all([p1, p2, p3, p4, p5, p6, p7, p8, p9, p10])
//   contractTypeList.value = resArray[0]['records']
//   contractCooperationList.value = resArray[1]['records']
//   contractFeetypeList.value = resArray[2]['records']
//   contractTemplateList.value = resArray[3]['records']
//   bankList.value = resArray[4]['records']
//   userList.value = resArray[5]['records']
//   governmentList.value = resArray[6]['records']
//   organizationList.value = resArray[7]['records']
//   contractSignerList.value = resArray[8]['records']
//   contractFiletypeList.value = resArray[9]['records']
// }

const initObj = async (val) =>  {
  oper.value = route.query.oper
  id.value = val
  if (id.value) {
    obj.value = await doGet(svc, `/contract-initial/${id.value}`, {}, true)
    // 验收条件
    let inspectionsList1 = await doGet(svc, '/contract-initial-inspection/listByField', {field: 'contractInitialId', values: id.value}, false)
    // 初始合同收款信息
    let payeesList1 = await doGet(svc, '/contract-initial-payee/listByField', {field: 'contractInitialId', values: id.value}, false)
    // 初始合同付款信息
    let paysList1 = await doGet(svc, '/contract-initial-pay/listByField', {field: 'contractInitialId', values: id.value}, false)
    // 参与方信息
    let participationsList1 = await doGet(svc, '/contract-initial-participation/listByField', {field: 'contractInitialId', values: id.value}, false)
    // 附件信息
    let oFiles1 = await doGet(svc, '/contract-initial-file/listByField', {field: 'contractInitialId', values: id.value}, false)
    console.log('oFiles1',oFiles1)
    oFiles.value = oFiles1.map(item => {
      item.original = item.attachmentName
      item.fileType = item.attachmentType
      return item
    })

    if (inspectionsList1.length > 0) {
      inspectionsList.value = inspectionsList1
    }

    if (payeesList1.length > 0) {
      payeesList.value = payeesList1
    }

    if (paysList1.length > 0) {
      paysList.value = paysList1
    }
    // console.log('participationsList1',participationsList1)
    if (participationsList1.length > 0) {
      partyAList.value = participationsList1.filter(item => item.participationType === '0')
      partyBList.value = participationsList1.filter(item => item.participationType === '1')
      partyCList.value = participationsList1.filter(item => item.participationType === '2')
    }
  }
}

watch(() => props.contractInitialId, async val => {
  await initObj(val)
  // await initList(val)
},{immediate:true})

const downloadFile1 = (row) => {
  doDownload(svc, '/customAttachment/download/' + row.attachmentId, {})
}
const viewFile1 = (row) => {
  attachmentVisible.value = true
  setTimeout(() => {
    previewAttachmentId.value = row.attachmentId
  },100)
}
</script>

<style scoped lang="scss">
.report-form-content {
  width: 100%;
  height: 52vh;
  overflow-x: hidden;
  overflow-y: auto;
}
.tabs-list-head-2 {
  padding-left: 0 !important;
}
.tabs-list-head-2 span {
  font-size: 22px !important;
  color: #1ac4ad !important;
}
.tabs-list-head {
  padding-left: 20px;
  display: flex;
  align-items: center;
}
.tabs-list-head span {
  font-size: 18px;
  color: #303133;
  font-weight: bold;
}
.tabs-list-head-1 {
  margin-bottom: 30px;
}
.tabs-list-type {
  display: flex;
  align-items: center;
  justify-content: center;
}
.update-log {
  width: 100%;
  margin: 30px 0;
}
.update-log-1 {
  margin-top: 0;
}
.steps {
  margin: 20px 0
}
.shenpilist {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--van-cell-background);
  box-sizing: border-box;
  width: 100%;
  padding: var(--van-cell-vertical-padding) var(--van-cell-horizontal-padding);
  overflow: hidden;
  color: var(--van-cell-text-color);
  font-size: var(--van-cell-font-size);
  // line-height: var(--van-cell-line-height);
  border-bottom: 1px solid var(--van-border-color);;
  & label {
    color: var(--van-field-label-color);
    // text-align: left;
    word-wrap: break-word;
    font-weight: 700;
  }
  & span {
    display: block;
    box-sizing: border-box;
    // width: 100%;
    color: var(--van-field-input-text-color);
    line-height: inherit;
    // text-align: left;
    background-color: transparent;
    border: 0;
    resize: none;
    -webkit-user-select: auto;
    user-select: auto;
  }
}
.shenpilist-head {
  color: var(--van-cell-text-color);
  font-size: var(--van-cell-font-size);
  font-weight: 700;
  padding: .3125rem 0;
  padding-left: 1.125rem;
  color: #337ecc;
}
.files-list {
  li {
    width: 100%;
    color: var(--van-gray-6);
    .files-list-1 {   
      .files-list-content {
        width: 95%;
        & p {
          padding-left: .9375rem;
          padding-top: .625rem;
          margin: 0;
          display: flex;
          align-items: center;
        }
        .list-content-hindline {
          font-size: 1.125rem;
          padding-right: .9375rem;
          font-weight: bold;
          color: #000;
        }
      }
    }
  }
}
</style>
